import { createRouter, createWebHashHistory } from 'vue-router'
import MainLayout from '@/layouts/MainLayout/index.vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    // 404页面匹配 - 回到首页
    {
      path: '/:pathMatch(.*)*',
      redirect: '/',
    },
    // 首页（首页不使用路由懒加载）
    {
      path: '/',
      name: 'Home',
      redirect: '/HomePage',
      meta: {
        title: '极简聊天室',
      },
      component: MainLayout,
      children: [
        {
          path: '/HomePage',
          name: 'HomePage',
          component: () => import('@/views/MainModule/HomePage/index.vue'),
          meta: {
            title: '极简聊天室',
          },
        },
      ],
    },
  ],
})

/**
 * 路由前置守卫
 *
 * 1. 路由跳转修改标题
 */
router.beforeEach(to => {
  document.title = `${to.meta.title} · ${import.meta.env.VITE_TITLE}`
})

export default router
